package com.baselibrary.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.baselibrary.R
import com.baselibrary.extension.checkValue
import com.baselibrary.utils.ContextUtils
import com.baselibrary.utils.GlideUtils
import kotlinx.android.synthetic.main.view_vip_heard_img_view.view.*

/**带VIP的头像
 * @author: merbng
 * @time: 2022/3/1 17:07
 * @description:
 */
class SmallVipHeardImgView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    init {
        initView(context)
        initTypeValue(context, attrs)
    }

    @SuppressLint("CustomViewStyleable")
    private fun initTypeValue(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.VipHeardViewAttrs)
        val vipWidth = typedArray.getDimension(R.styleable.VipHeardViewAttrs_vipWidth, 0f).toInt()
        val vipHeight = typedArray.getDimension(R.styleable.VipHeardViewAttrs_vipHeight, 0f).toInt()
        val heardWidth =
            typedArray.getDimension(R.styleable.VipHeardViewAttrs_heardWidth, 0f).toInt()
        val heardHeight =
            typedArray.getDimension(R.styleable.VipHeardViewAttrs_heardHeight, 0f).toInt()
        civ_heard.layoutParams.width = heardWidth
        civ_heard.layoutParams.height = heardHeight

        img_vip_tag.layoutParams.width = vipWidth
        img_vip_tag.layoutParams.height = vipHeight
        typedArray.recycle()
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.view_small_vip_heard_img_view, this, true)
    }

    fun setData(url: String, isShowVip: Boolean = false) {
        img_vip_tag.visibility = if (isShowVip) View.VISIBLE else View.INVISIBLE
        GlideUtils.loadUserHead(ContextUtils.getCurApplication().applicationContext, url.checkValue(), civ_heard)
        civ_heard.borderColor = Color.parseColor(if (isShowVip) "#F5D141" else "#00000000")
    }

    fun setData(url: String, isShowVip: Boolean = false, isGon: Int) {
        img_vip_tag.visibility = if (isShowVip) View.VISIBLE else isGon
        GlideUtils.loadUserHead(ContextUtils.getCurApplication().applicationContext, url, civ_heard)
        civ_heard.borderColor = Color.parseColor(if (isShowVip) "#F5D141" else "#00000000")
    }

    fun setData(bitmap: Bitmap, isShowVip: Boolean = false, isGon: Int) {
        img_vip_tag.visibility = if (isShowVip) View.VISIBLE else isGon
        civ_heard.setImageBitmap(bitmap)
        civ_heard.borderColor = Color.parseColor(if (isShowVip) "#F5D141" else "#00000000")
    }

    fun setDefault(id: Int, isShowVip: Boolean = false) {
        img_vip_tag.visibility = if (isShowVip) View.VISIBLE else View.INVISIBLE
        civ_heard.setImageResource(id)
        civ_heard.borderColor = Color.parseColor(if (isShowVip) "#F5D141" else "#00000000")
    }

    fun setData(resId: Int, isShowVip: Boolean = false) {
        img_vip_tag.visibility = if (isShowVip) View.VISIBLE else View.INVISIBLE
        setBackgroundResource(resId)
        civ_heard.borderColor = Color.parseColor(if (isShowVip) "#F5D141" else "#00000000")
    }

    fun setParams(heardWidth: Int, heardHeight: Int, vipWidth: Int, vipHeight: Int) {
        val vipParams: ViewGroup.LayoutParams = img_vip_tag.layoutParams
        val headParams: ViewGroup.LayoutParams = civ_heard.layoutParams

        headParams.height = heardHeight
        headParams.width = heardWidth
        vipParams.height = vipHeight
        vipParams.width = vipWidth
        img_vip_tag.layoutParams = vipParams
        civ_heard.layoutParams = headParams
    }
}